package com.stx.friends.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.friends.dao.UserDao;
import com.stx.friends.entity.UserEntity;
import com.stx.friends.vo.UserVo;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {

	/**
	 * @param args
	 */
	

	public List<UserEntity> list(UserVo vo) {

		String sql = "select * from wrx_user";
		JdbcTemplate jt = getJdbcTemplate();
		List<UserEntity> userList = jt.query(sql, new UserRowMapper());

		return userList;
	}
	@Override
	public List<UserEntity> listByUid(String wrxUid) {
		String sql = "select * from wrx_user where uid=?";
		Object[] args={wrxUid};
		JdbcTemplate jt = getJdbcTemplate();
		List<UserEntity> userList = jt.query(sql,args, new UserRowMapper());
		return userList;
	}
	class UserRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			UserEntity entity = new UserEntity();

			entity.setWrxUid(rs.getString("wrxUid"));
			entity.setUname(rs.getString("uname"));
			entity.setBlogName(rs.getString("blogName"));
			entity.setPassword(rs.getString("password"));
			entity.setEmail(rs.getString("email"));
			entity.setDescription(rs.getString("description"));
			entity.setLastModifyTime(rs.getString("lastModifyTime"));
			entity.setDelFlag(rs.getString("delFlag"));

			return entity;
		}

	}

	@Override
	public void delUser(String wrxUid) {

		String sql = "delete from wrx_user where wrxUid = ?";
		JdbcTemplate jt = getJdbcTemplate();
		// 构建参数数组
		Object[] args = { wrxUid };

		jt.update(sql, args);

	}

	@Override
	public void addUser(UserVo user) {

		String sql = "insert into wrx_user (wrxUid,uname,blogName,password,email,description) values(seq_wrx_user.nextval,?,?,?,?,?)";
		JdbcTemplate jt = getJdbcTemplate();
		// 构建参数数组
		Object[] args = { user.getUname(), user.getBlogName(),
				user.getPassword(), user.getEmail(), user.getDescription() };

		jt.update(sql, args);

	}

	@Override
	public UserEntity getUserById(String uid) {
		String sql = "select * from wrx_user where wrxuid=" + uid;
		JdbcTemplate jt = getJdbcTemplate();
		return (UserEntity) jt.query(sql, new UserRowMapper()).get(0);
	}

	@Override
	public UserEntity findUserByUnameAndPassword(String username,
			String password) {
		String sql = "select * from wrx_user where uname='" + username
				+ "'and password='" + password + "'";
		JdbcTemplate jt = getJdbcTemplate();
		UserEntity userentity = null;
		List<UserEntity> list = jt.query(sql, new UserRowMapper());
		if (list.size() > 0) {
			userentity = list.get(0);

		}
		return userentity;
	}

	@Override
	public String getIdByUname(String uname) {
		String sql = "select wrxuid from wrx_user where uname='" + uname+"'";
		System.out.println(uname);
		JdbcTemplate jt = getJdbcTemplate();	
		String id="";
		try {
			id=Integer.toString(jt.queryForInt(sql));
		} catch (Exception e) {
			id="";
		}
		
		
		System.out.println(id);
		return id;
	}
	@Override
	public void updateUser(UserVo criteria) {
		JdbcTemplate jt = getJdbcTemplate();
		
		String sql = "update  wrx_user set blogname=?,password=?,email=?,description=? where wrxuid=? " ;

		// 构建参数数组
		System.out.println(criteria.getBlogName()+criteria.getPassword()+criteria.getEmail()+criteria.getDescription()+criteria.getwrxUid());
		Object[] args = {criteria.getBlogName(),criteria.getPassword(),criteria.getEmail(),criteria.getDescription(),criteria.getwrxUid() };

		jt.update(sql, args);
		
	}

	

}
